package com.tenton.controller;

import com.tenton.MyException.DataNotExist;
import com.tenton.pojo.ImCommodity;
import com.tenton.service.CommodityService;
import com.tenton.service.ImCommodityService;
import com.tenton.service.StockService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@ApiOperation("管理员商品进货管理")
@RequestMapping("/admin/flatform/imCommodity")
public class AImCommodityController {
    @Autowired
    ImCommodityService imCommodityService;
    @Autowired
    StockService stockService;
    @Autowired
    CommodityService commodityService;

    /**
     * 获取所有进货信息
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("获取所有进货信息")
    @GetMapping("/getAll")
    public String getAll(ModelMap model,
                         @ApiParam("页码") @RequestParam(defaultValue="1",required=true,value="pn") Integer pn
    ) {
        imCommodityService.getAll(model,pn);
        return "admin/commodity/getImCommodityList";
    }

    /**
     * 根据id查询单个商品信息
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("根据id查询单个商品信息")
    @PostMapping("/getImCommodity")
    public String getImCommodity(int id,Model model){
        imCommodityService.selectByPrimaryKey(id,model);
        return "admin/commodity/getImCommodity";
    }

    /**
     * 删除商品信息
     * @param id
     * @param session
     * @return
     */
    @ApiOperation("通过Id删除商品信息")
    @GetMapping("/delete")
    public String delete(int id,HttpSession session){
        imCommodityService.deleteByPrimaryKey(id,session);
        return "redirect:/admin/flatform/imCommodity/getAll";
    }

    /**
     * 跳转到增加页面
     * @return
     */
    @ApiOperation("跳转到增加页面")
    @GetMapping("/toAdd")
    public String toAdd (){
        return "admin/commodity/addImCommodity";

    }

    /**
     * 先判断数据库有没有，没有就新增
     * 同时需要将之加入到仓库
     * @param imCommodity
     * @param session
     * @param request
     * @return
     */
    @ApiOperation("新增进货信息")
    @PostMapping("/insert")
    public String insert (ImCommodity imCommodity, HttpSession session, HttpServletRequest request){
        try {
            imCommodityService.insert(imCommodity,session);
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "进货信息为空！");
            return "admin/commodity/addImCommodity";
        }
        return "redirect:/admin/flatform/imCommodity/getAll";
    }

    /**
     * 时间格式化
     * @param binder
     */
    @InitBinder
    protected void init(ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    /**
     * 商品名称模糊查询
     * @param model
     * @param comName
     * @param pn
     * @return
     */
    @ApiOperation("商品名称模糊查询")
    @GetMapping("/getByParams")
    public String getByParams(Model model,
                             @ApiParam("商品名称") @RequestParam(value="comName",required=false)String comName,
                             @ApiParam("页码") @RequestParam(defaultValue="1",required=true,value="pn") Integer pn
    ) {
        imCommodityService.getByParams(comName,model,pn);
        return "admin/commodity/getImCommodityByParams";
    }

    /**
     * 查询商品信息
     * @param comId
     * @param model
     * @return
     */
    @ApiOperation("通过comId查询商品信息")
    @GetMapping("/getCommodityByComId")
    public String getCommodityByComId(int comId,Model model, HttpServletRequest request){
        try {
            commodityService.getCommodityByComId(comId,model);
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "商品信息不存在！");
        }
        return "admin/commodity/addImCommodity";
    }
}
